package com.iotproject.studyservice.service.impl;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyGrade;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.UserRememberWord;
import com.iotproject.studyservice.entity.vo.SelectWord;
import com.iotproject.studyservice.mapper.StudyWordMapper;
import com.iotproject.studyservice.service.SelectWordService;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserRememberWordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelectWordServiceImpl implements SelectWordService {

    @Autowired
    StudyGradeService studyGradeService;

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    StudyWordMapper studyWordMapper;

    @Autowired
    UserRememberWordService userRememberWordService;

    //获取 背单词 界面列表
    @Override
    public R getSelectWordList(HttpServletRequest httpServletRequest) {
        List<SelectWord> list = new ArrayList<>();
        List<StudyGrade> studyGradeList = studyGradeService.list(null);//等级列表
        //获取封面
        //获取所有单词数
        //获取用户id
        //获取已记住单词数
        for (int i = 0; i < studyGradeList.size(); i++) {
            StudyGrade studyGrade = studyGradeList.get(i);
            QueryWrapper<StudyWord> studyWordQueryWrapper = new QueryWrapper<>();
            studyWordQueryWrapper.eq("grade", studyGrade.getId());
            int wordNumber = studyWordService.count(studyWordQueryWrapper);
            String memberUserId = studyWordService.getMemberUserId(httpServletRequest);

            QueryWrapper<UserRememberWord> userRememberWordQueryWrapper = new QueryWrapper<>();
            userRememberWordQueryWrapper.eq("user_id", memberUserId);
            userRememberWordQueryWrapper.eq("grade", studyGrade.getId());
            int rememberNumber = userRememberWordService.count(userRememberWordQueryWrapper);
            //赋值
            SelectWord selectWord = new SelectWord();
            selectWord.setId(studyGrade.getId());//等级id
            selectWord.setName(studyGrade.getName());
            selectWord.setCover(studyGrade.getCover());
            selectWord.setRememberNumber(rememberNumber);
            selectWord.setWordNumber(wordNumber);
            list.add(selectWord);
        }
        return R.ok().data("list", list);
    }

    @Override
    public StudyWord queryStudyWordByGrade(String grade, HttpServletRequest httpServletRequest) {
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        StudyWord studyWord = studyWordMapper.queryStudyWordByGrade(grade, memberUserId);
        return studyWord;
    }

    @Override
    public StudyWord queryStudyWordByGradeRandom(String grade) {
        StudyWord studyWord = studyWordMapper.queryStudyWordByGradeRandom(grade);
        return studyWord;
    }


}
